﻿
using System;
using System.IO;
using System.Collections;
//This function calculates the angle the missile launcher should shoot at in degrees.  
//Z is the height, and X is the horizontal distance away the object is from the missile launcher.
//This program assumes that the missile launcher is positioned at coordinate (0,0) for both x, and z.
//This program will only give angles 0-90
//X, and Y are given in meters
class AngleCalculation
{
    private double initialVelocity;
    private double horizontalAngle;
    private double verticalAngle;
    public AngleCalculation()
    {
        initialVelocity = 53.67;
    }
    void setInitialVelocity(double velocity)
    {
        initialVelocity = velocity;
    }
    public double radiansToDegrees(double radians)
    {
        return radians * (180 / Math.PI);
    }
    public double degreesToRadians(double degrees)
    {
        return degrees * (Math.PI / 180);
    }
    public double cos(double degrees)
    {
        return Math.Cos(degreesToRadians(degrees));
    }
    public double sin(double degrees)
    {
        return Math.Sin(degreesToRadians(degrees));
    }
    public double getHorizontalAngle(double finalX, double finalZ)
    {
        horizontalAngle = radiansToDegrees(Math.Atan(finalX / finalZ));
        return horizontalAngle;
    }



    public double getVerticalAngle(double diffY, double finalZ, double velocity = 136.33)
    {
        double rightSide;
        double leftSide;
        double difference;
        double bestDiff;
        initialVelocity = velocity;
        bestDiff = double.MaxValue;
        for (int angle = 0; angle <= 90; angle++)
        {
//            Console.WriteLine("\n\n\n\n\n\n\n");
            Console.WriteLine("angle is {0}", angle);
//            Console.WriteLine("diffY {0}\n", diffY);
//            Console.WriteLine("initialVelocity {0}\n", initialVelocity);
//            Console.WriteLine("cos(angle) {0}\n", cos(angle));
            leftSide = diffY * 2 * Math.Pow(initialVelocity, 2) * Math.Pow(cos(angle), 2);
 //           Console.WriteLine("left side is {0}", leftSide);
            rightSide = 2 * cos(angle) * sin(angle) * Math.Pow(initialVelocity, 2) * finalZ - 386.22 * Math.Pow(finalZ, 2);
 //           Console.WriteLine("right side is {0}", rightSide);
            difference = Math.Abs(leftSide - rightSide);
            Console.WriteLine("difference is {0}", difference);

            if (angle == 0)
            {
                bestDiff = difference;
                verticalAngle = angle;
            }
            else if (difference < bestDiff)
            {
                bestDiff = difference;
                verticalAngle = angle;
            }
//            Console.WriteLine("best angle is {0}", verticalAngle);
 //           Console.WriteLine("best difference is {0}", bestDiff);
        }
        Console.WriteLine("returned angle is {0}", verticalAngle);
        return verticalAngle;
    }
}
class hello
{

    public static void Main()
    {
        AngleCalculation calc = new AngleCalculation();
        /*    Console.WriteLine("Please enter the x value");
            string input = Console.ReadLine();
            double val1 = double.Parse(input);
            Console.WriteLine("Please enter the y value");
            input = Console.ReadLine();
            double val2 = double.Parse(input);
            double a = calc.getHorizontalDegrees(val1,val2);
            Console.WriteLine("{0}\n", a);
          */
        string input;
        double val1;
        double val2;
        double vel;
        double a;
        while (true)
        {
            Console.WriteLine("Please enter the y value");
            input = Console.ReadLine();
            val1 = double.Parse(input);
            Console.WriteLine("Please enter the z value");
            input = Console.ReadLine();
            val2 = double.Parse(input);
            Console.WriteLine("Please enter the initial velocity");
            input = Console.ReadLine();
            vel = double.Parse(input);
            a = calc.getVerticalDegrees(val1, val2, vel);
            Console.WriteLine("{0}\n", a);
        }



    }




}
